<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row class="operation">
            <Form inline :label-width="170" ref="form" :model="form" label-position="right">
              <Button @click="getDataList" icon="md-refresh">刷新</Button>
              <!-- <Button @click="add" type="primary" icon="md-add">添加</Button> -->
              <!-- <FormItem label="是否过期" prop="pastDue">
                <Select v-model="searchForm.pastDue" clearable style="width:200%">
                  <Option value="YES">已过期</Option>
                  <Option value="NO">未过期</Option>
                </Select>
              </FormItem> -->
              <!-- <Button @click="handleSearch" type="primary" icon="ios-search" style="margin-left:90px;">搜索</Button> -->
            </Form>
          </Row>
          
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <!-- <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row> -->
        </Card>
      </Col>
    </Row>
    
  </div>
</template>

<script>
import circleLoading from "@/views/my-components/circle-loading.vue";
export default {
  name: "couponAdminManage",
  components: {
    circleLoading,
  },
  data() {
    return {
      loading: true, // 表单加载状态
      loadingEdit: false,
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalTitle: "", // 添加或编辑标题
      searchForm: { // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        // pastDue: "",
      },
      selectData:[],
      // 表单验证规则
      submitLoading: false, // 添加或编辑提交状态
      columns: [
        // 表头
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "优惠券名称",
          key: "title",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "类型",
          key: "disCouponType",
          minWidth: 90,
          sortable: false,
        },
        {
          title: "满多少",
          key: "fullMoney",
          minWidth: 80,
          sortable: false,
        },
        {
          title: "减多少",
          key: "subtractMoney",
          minWidth: 80,
          sortable: false,
        },
        {
          title: "有效期",
          key: "validityPeriod",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          minWidth: 120,
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                    // icon: "md-trash"
                  },
                  style: {
                    marginRight: "5px",
                    display: (params.row.join === 0) ? "inline-block" : "none"
                  },
                  on: {
                    click: () => {
                      this.removeJoin(params.row);
                    }
                  }
                },
                "退出活动"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    // icon: "md-trash"
                  },
                  style: {
                    marginRight: "5px",
                    display: (params.row.join === 1) ? "inline-block" : "none"
                  },
                  on: {
                    click: () => {
                      this.addJoin(params.row);
                    }
                  }
                },
                "参加活动"
              ),
            ]);
          }
        }
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      type: ""
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    handleSearch () {
      this.searchForm.pageNumber = 1;
      this.getDataList()
    },

    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
        // if (this.searchForm.pastDue == undefined) {
        //     this.searchForm.pastDue = ""
        // }
        this.loading = true;
        // 带多条件搜索参数获取表单数据 请自行修改接口
        this.postRequest("/discountCoupon/getPlatformDiscountCoupon", this.searchForm).then(res => {
            this.loading = false;
            if (res.success) {
              this.data = res.result;
            //   this.total = res.result.size;
            } else {
              this.data=[];
            //   this.total=0;
            }
        });
    },
    add() {
      this.modalType = 0;
      this.modalTitle = "添加";
      this.$refs.form.resetFields();
      delete this.form.id;
      if (this.form.goodCategoryName == "") {

      } else {
        let data = this.$refs.treeOne.getSelectedNodes()[0];
        if (data) {
          data.selected = false;
        }
      }
      
      this.form.goodCategoryName = "";
      this.modalVisible = true;
    },
    
    removeJoin(v){
      this.$Modal.confirm({
        title: "确认退出",
        // 记得确认修改此处
        content: "您确认要退出该活动吗?",
        loading: true,
        onOk: () => {
          var param={
            ids:v.id
          }
          this.postRequest("/discountShop/removeDiscountShop",param).then(res => {
            this.loading = false;
            if (res.success) {
              this.$Message.success("操作成功");
              this.$Modal.remove();
              this.getDataList();
            } else {
              this.$Message.error(res.message);
              this.$Modal.remove();
            }
          });
        }
      });
    },
    addJoin (v) {
      this.$Modal.confirm({
        title: "确认退出",
        // 记得确认修改此处
        content: "您确认要退出该活动吗?",
        loading: true,
        onOk: () => {
          var param={
            discountId:v.id,
            shopId: JSON.parse(this.getStore("userInfo")).id
          }
          this.postRequest("/discountShop/insertOrderUpdateDiscountShop",param).then(res => {
            this.loading = false;
            if (res.success) {
              this.$Message.success("操作成功");
              this.$Modal.remove();
              this.getDataList();
            } else {
              this.$Message.error(res.message);
              this.$Modal.remove();
            }
          });
        }
      });
    },
    delAll() {
        console.log(this.data)
        if (this.selectCount <= 0) {
            this.$Message.warning("您还未选择要删除的数据");
            return;
        }
        this.$Modal.confirm({
            title: "确认删除",
            content: "您确认要删除所选的数据?",
            loading: true,
            onOk: () => {
            // 批量删除
                this.postRequest("/size/removeSize",{id:this.brandId}).then(res => {
                    this.$Modal.remove();
                    if (res.success) {
                        this.$Message.success("操作成功");
                        this.getDataList();
                    }
                })
            }
        })
    }
},
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>